package com.adcc.ags.gms.user.vo;

import com.adcc.ags.gms.core.entity.BaseEntity;
import com.adcc.ags.gms.core.vo.BaseVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 用户树型视图
 */
public class GWUserTreeVO extends BaseVO{

    // 用户树型节点
    private List<GWUserNodeVO> nodeList = Lists.newArrayListWithExpectedSize(1000);

    /**
     * 构造函数
     */
    public GWUserTreeVO(){

    }

    /**
     * 构造函数
     */
    public GWUserTreeVO(List<GWUserNodeVO> list){
        nodeList = list;
    }

    public List<GWUserNodeVO> getNodeList() {
        return nodeList;
    }

    public void setNodeList(List<GWUserNodeVO> nodeList) {
        this.nodeList = nodeList;
    }

    @Override
    public BaseEntity toEntity() throws ParseException {
        return null;
    }

    @Override
    public String toJson() throws JsonProcessingException {
        Map<String,Object> map = Maps.newHashMapWithExpectedSize(2);
        map.put("id",0);
        map.put("text","网关用户列表");
        Map<String,Object> stateMap = Maps.newHashMapWithExpectedSize(1);
        stateMap.put("opened",true);
        map.put("state",stateMap);
        if(nodeList != null && nodeList.size() > 0){
            List<Map<String,Object>> list = Lists.newArrayListWithExpectedSize(1000);
            for(GWUserNodeVO vo : nodeList){
                Map<String,Object> childMap = Maps.newHashMapWithExpectedSize(2);
                childMap.put("id", vo.getId());
                childMap.put("text", vo.getText());
                Map<String,Object> selectedMap  = Maps.newHashMapWithExpectedSize(1);
                selectedMap.put("selected",vo.isSelected());
                childMap.put("state",selectedMap);
                list.add(childMap);
            }
            map.put("children",list);
        }
        return new ObjectMapper().writeValueAsString(map);
    }
}
